'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: upload_file_schema.py
* @Time: 2025/11/12
* @All Rights Reserve By Brtc
'''
from flask_wtf import FlaskForm
from flask_wtf.file import FileRequired, FileSize, FileAllowed
from marshmallow import Schema, fields, pre_dump
from wtforms import FileField

from internal.entity.upload_file_entity import ALLOWED_FILE_EXTENSION
from internal.model import UploadFile


class UploadFileReq(FlaskForm):
    """上传文件请求"""
    file = FileField("file", validators=[
        FileRequired("上传文件不能为空"),
        FileSize(max_size = 16*1024*1024, message="上传文件最大不超过15MB"),
        FileAllowed(ALLOWED_FILE_EXTENSION, message=f"仅允许上传 {'/'.join(ALLOWED_FILE_EXTENSION)} 格式的文件")
    ])


class UploadFileResp(Schema):
    """上传文件接口返回"""
    id = fields.UUID(dump_default="")
    account_id = fields.UUID(dump_default="")
    name = fields.String(dump_default="")
    key = fields.String(dump_default="")
    size = fields.Integer(dump_default=0)
    extension = fields.String(dump_default="")
    mime_type = fields.String(dump_default="")
    created_at = fields.Integer(dump_default=0)

    @pre_dump
    def precess_data(self, data:UploadFile, **kwargs):
        return {
            "id": data.id,
            "account_id": data.account_id,
            "name": data.name,
            "key": data.key,
            "size": data.size,
            "extension": data.extension,
            "mime_type": data.mime_type,
            "created_at": int(data.created_at.timestamp()),
        }